package by.admonko.universaltesting.dataaccess.impl;

import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import by.admonko.universaltesting.dataaccess.QuestionDao;
import by.admonko.universaltesting.datamodel.Question;
import by.admonko.universaltesting.datamodel.Question_;
import by.admonko.universaltesting.datamodel.Test;

@Repository
public class QuestionDaoImpl extends AbstractDaoImpl<Integer, Question>
		implements QuestionDao {

	protected QuestionDaoImpl() {
		super(Question.class);
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<Question> getByTest(Test test) {// Refactoring!!!
		CriteriaBuilder cBuilder = getEm().getCriteriaBuilder();

		CriteriaQuery<Question> root = cBuilder.createQuery(Question.class);
		Root<Question> criteria = root.from(Question.class);

		root.select(criteria);

		root.where(cBuilder.equal(criteria.get(Question_.test), test));

		TypedQuery<Question> query = getEm().createQuery(root);
		List<Question> list = query.getResultList();
		return list;
	}

}
